package com.example.medicalrecord.AggregationModule.loginRegistered.service.impl;

import com.example.medicalrecord.AggregationModule.loginRegistered.mapper.LoginRegisteredMapper;
import com.example.medicalrecord.AggregationModule.loginRegistered.pojo.dto.AdminLoginParam;
import com.example.medicalrecord.AggregationModule.loginRegistered.pojo.dto.AdminRegParam;
import com.example.medicalrecord.AggregationModule.loginRegistered.pojo.entity.PatientLoginReg;
import com.example.medicalrecord.AggregationModule.loginRegistered.pojo.vo.AdminLoginVO;
import com.example.medicalrecord.AggregationModule.loginRegistered.service.LoginRegisteredService;
import com.example.medicalrecord.base.exception.ServiceException;
import com.example.medicalrecord.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class LoginRegisteredServiceImp implements LoginRegisteredService {
    @Autowired
    private LoginRegisteredMapper loginRegisteredMapper;
    /**
     * 登录模块
     * @param userLoginParam
     * @return
     */
    @Override
    public AdminLoginVO login(AdminLoginParam userLoginParam) {
        log.debug("用户登录:userLogin={}", userLoginParam);
            AdminLoginVO adminLoginVO = new AdminLoginVO();
            String phone = userLoginParam.getPhone();
            String sole = userLoginParam.getSole();
            System.out.println(sole);

            /**
             * 判断是否为管理员
             */
            if (sole.equals("10")) {
                System.out.println("进入管理员程序");
                adminLoginVO = loginRegisteredMapper.selectAdminPhone(phone);
                /*判断账号是否注册*/
                if (adminLoginVO == null) {
                    System.out.println(111);
                    throw new ServiceException(StatusCode.NOT_REGISTERED);
                }
                /*判断电话是否输入正确*/
                if (!adminLoginVO.getPhone().equals(userLoginParam.getPhone())) {
                    throw new ServiceException(StatusCode.USERNAME_ERROR);
                }
                /*判断密码是否相同*/
                if (!adminLoginVO.getPassword().equals(userLoginParam.getPassword())) {
                    throw new ServiceException(StatusCode.PASSWORD_ERROR);
                }
            }
            /**
             * 判断是否为医生
             */
            else if (sole.equals("20")) {
                System.out.println("进入医生程序");
                adminLoginVO = loginRegisteredMapper.selectDoctorPhone(phone);
                /*判断账号是否注册*/
                if (adminLoginVO == null) {
                    throw new ServiceException(StatusCode.NOT_REGISTERED);
                }
                /*判断电话是否输入正确*/
                if (!adminLoginVO.getPhone().equals(userLoginParam.getPhone())) {
                    throw new ServiceException(StatusCode.USERNAME_ERROR);
                }
                /*判断密码是否相同*/
                if (!adminLoginVO.getDoctorPassword().equals(userLoginParam.getPassword())) {
                    throw new ServiceException(StatusCode.PASSWORD_ERROR);
                }
            }
            /**
             * 判断是否为患者
             */
            else if (sole.equals("30")) {
                System.out.println("进入患者程序...");
                adminLoginVO = loginRegisteredMapper.selectPatientPhone(phone);
                /*判断账号是否注册*/
                if (adminLoginVO == null) {
                    throw new ServiceException(StatusCode.NOT_REGISTERED);
                }
                /*判断电话是否输入正确*/
                if (!adminLoginVO.getPhone().equals(userLoginParam.getPhone())) {
                    throw new ServiceException(StatusCode.USERNAME_ERROR);
                }
                /*判断密码是否相同*/
                if (!adminLoginVO.getPatientPassword().equals(userLoginParam.getPassword())) {
                    throw new ServiceException(StatusCode.PASSWORD_ERROR);
                }
            }
            System.out.println(adminLoginVO);
            return adminLoginVO;

    }
    /**
     * 注册模块
     * @param userRegParam
     */
    @Override
    public void regAdd(AdminRegParam userRegParam) {
        log.debug("业务注册:userRegParam={}",userRegParam);
        PatientLoginReg patientLoginReg = new PatientLoginReg();
        BeanUtils.copyProperties(userRegParam,patientLoginReg);
        patientLoginReg.setCreateTime(new Date());
        patientLoginReg.setInpatientStatus("0");
        patientLoginReg.setSole("30");
        loginRegisteredMapper.regAdd(patientLoginReg);
    }
}

